package com.why.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.why.blog.dao.mapper.HaloUserMapper;
import com.why.blog.dao.mapper.UserRoleMapper;
import com.why.blog.entity.HaloUser;
import com.why.blog.entity.SysUserRole;
import com.why.blog.entity.VO.HaloUserVO;
import com.why.blog.service.HaloUserService;
import org.apache.commons.lang3.StringUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TriggerWu
 * @since 2019-04-28
 */
@Service
public class HaloUserServiceImpl implements HaloUserService {


    @Autowired
    private Mapper beanmapper;
    @Autowired
    private HaloUserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public HaloUser findUser(String userId) {
        if (StringUtils.isBlank(userId)) {
            return null;
        }
        return userMapper.selectOne(new QueryWrapper<HaloUser>().eq("user_id", userId));

    }

    @Override
    public HaloUser loginUser(HaloUser user) {
        HaloUser one = null;
        if(user ==null){
            return null;
        }
        if(StringUtils.isNotBlank(user.getUserName())&&StringUtils.isNotBlank(user.getUserPass())){
            one = userMapper.selectOne(new QueryWrapper<HaloUser>().eq("user_name", user.getUserName()).eq("user_pass", user.getUserPass()));
        }
        if(one!=null){
            return one;
        }
        return null;
    }

    @Override
    public HaloUser findUserRoleByName(String userName) {
        if(StringUtils.isBlank(userName)){
            return  null;
        }
        return userMapper.selectOne(new QueryWrapper<HaloUser>().eq("user_name", userName));

    }

    @Override
    public Integer saveUser(HaloUserVO uvo) {
        SysUserRole ur = new SysUserRole();
        ur.setSysUserId(uvo.getUserId());
        //统一user权限
        ur.setSysUserRole("2");
        userRoleMapper.insert(ur);
        HaloUser user = beanmapper.map(uvo, HaloUser.class);
        return userMapper.insert(user);
    }


}
